/*
 * @Descripttion:
 * @version: 版本
 * @Author: Simon
 * @Date: 2020-04-01 18:24:22
 * @LastEditors: Simon
 * @LastEditTime: 2020-09-10 10:30:39
 */
/**
 * 时间戳的00:00
 * @param {*} ms
 */
export const dayStartTime = (ms) => {
  let t = new Date(new Date(ms).toDateString()).getTime()
  return t
}

export const dayEndTime = (ms) => {
  let t = new Date(new Date(ms).toDateString()).getTime() + 24 * 3600 * 1000 - 1
  return t
}

/**
 *
 * @param timestamp
 * @returns {string}
 */
export const formatTs = function(timestamp) {
  // console.log(timestamp)
  const t = new Date(timestamp).toLocaleDateString()
  return t
}

export const formatTimeStamp = function(timestamp) {
  // console.log(timestamp, typeof timestamp)
  if (timestamp) {
    if (typeof timestamp === 'string') {
      if (timestamp.length > 10) {
        return formatMS(parseInt(timestamp))
      } else if (timestamp.length === 10) {
        return formatSecond(parseInt(timestamp))
      } else {
        return timestamp
      }
    } else {
      if (timestamp.toString().length > 10) {
        return formatMS(timestamp)
      } else if (timestamp.toString().length === 10) {
        return formatSecond(timestamp)
      } else {
        return timestamp
      }
    }
  }
}

/**
   *  e.x. 2018-02-10 10:25:52
   * @param time
   * @returns {string}
   */
export const formatSecond = function(timeSecond) {
  const time = new Date(timeSecond * 1000)
  // console.log(timeSecond)
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  let day = time.getDate()
  let hour = time.getHours()
  let min = time.getMinutes()
  let second = time.getSeconds()
  if (month < 10) {
    month = `0${month}`
  }
  if (day < 10) {
    day = `0${day}`
  }
  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }
  if (second < 10) {
    second = `0${second}`
  }
  const t = `${year}-${month}-${day} ${hour}:${min}:${second}`
  return t
}

export const formatMS = function(ms) {
  const time = new Date(ms)
  // console.log(timeSecond)
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  let day = time.getDate()
  let hour = time.getHours()
  let min = time.getMinutes()
  let second = time.getSeconds()
  if (month < 10) {
    month = `0${month}`
  }
  if (day < 10) {
    day = `0${day}`
  }
  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }
  if (second < 10) {
    second = `0${second}`
  }
  const t = `${year}-${month}-${day} ${hour}:${min}:${second}`
  return t
}

/**
   * 根据当前时间，获取时间范围
   * 最近7天
   * getTimeRange(7)
   * @param days
   */
export const getTimeRange = function(days) {
  console.log('getTimeRange>>', days)
  const date = getLocalDateString()

  console.log('t>>>___', date)
  const endTime = new Date(date).getTime() + 3600 * 1000 * 24

  const startTime = endTime - 3600 * 1000 * 24 * days
  console.log(startTime, endTime)
  return [parseInt(startTime.toFixed(0)), parseInt(endTime.toFixed(0))]
}

export const getTimeRangeStr = function(days) {
  console.log('getTimeRange>>', days)
  const date = getLocalDateString()

  console.log('t>>>___', date)
  const endTime = new Date(date).getTime() + 3600 * 1000 * 24

  const startTime = endTime - 3600 * 1000 * 24 * days
  console.log(startTime, endTime)
  return [toYYYYMMDD(parseInt(startTime.toFixed(0))), toYYYYMMDD(parseInt(endTime.toFixed(0)))]
}
// 当前日期
export const getLocalDateString = function() {
  const t = new Date()
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let date = t.getDate()
  if (date < 10) {
    date = '0' + date
  }
  const r = `${year}-${month}-${date}`
  // eslint-disable-next-line no-irregular-whitespace
  const hm = new Date(r).getTime()
  return hm
}

export const getNowdate = function() {
  const t = new Date()
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let date = t.getDate()
  if (date < 10) {
    date = '0' + date
  }
  const r = `${year}-${month}-${date}`
  return r
}

export const getBeginMonth = function() {
  const t = new Date()
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let date = t.getDate()
  if (date < 10) {
    date = '0' + date
  }
  const r = `${year}-${month}-01`
  // eslint-disable-next-line no-irregular-whitespace
  const hm = new Date(r).getTime()
  return hm
}
/**
   * 昨天日期
   */
export const getLocaltodyString = function() {
  const t = new Date()
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = `0${month}`
  }
  const date = t.getDate()
  const r = `${year}-${month}-${date} 00:00:00`
  return new Date(r).getTime()
}

export const toHHMM = function (ms) {
  let ts = null
  // console.log(isNaN(ms))
  if (typeof ms === 'string') {
    if (isNaN(ms)) {
      ts = ms
    } else {
      ts = Number(ms)
    }
  } else {
    ts = ms
  }
  let time = new Date(ts)
  // console.log(time)
  let hour = time.getHours()
  let min = time.getMinutes()

  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }

  let t = `${hour}:${min}`
  return t
}

export const toMMDD = function (ms) {
  let time = new Date(ms)
  let month = time.getMonth() + 1
  let date = time.getDate()

  if (month < 10) {
    month = `0${month}`
  }
  if (date < 10) {
    date = `0${date}`
  }

  let t = `${month}-${date}`
  return t
}

export const toYYYYMMDD = function(time) {
  const t = new Date(time)
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = `0${month}`
  }
  let date = t.getDate()
  if (date < 10) {
    date = `0${date}`
  }
  const r = `${year}-${month}-${date}`
  return r
}


export const toMMDDHHMM = function (ms) {
  let time = new Date(ms)
  // console.log(timeSecond)
  // let year = time.getFullYear()
  let month = time.getMonth() + 1
  let day = time.getDate()
  let hour = time.getHours()
  let min = time.getMinutes()
  let second = time.getSeconds()
  if (month < 10) {
    month = `0${month}`
  }
  if (day < 10) {
    day = `0${day}`
  }
  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }
  if (second < 10) {
    second = `0${second}`
  }
  let t = `${month}-${day} ${hour}:${min}`
  return t
}

export const toYMDHMS = function(time) {
  const t = new Date(time)
  const year = t.getFullYear()
  let month = t.getMonth() + 1
  if (month < 10) {
    month = `0${month}`
  }
  let date = t.getDate()
  if (date < 10) {
    date = `0${date}`
  }

  let hour = t.getHours()
  let min = t.getMinutes()
  let s = t.getSeconds()
  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }
  if (s < 10) {
    s = `0${s}`
  }
  const r = `${year}-${month}-${date} ${hour}:${min}:${s}`
  return r
}

export const formatDate = function(value) {
  const date = new Date(value)
  const y = date.getFullYear()
  let MM = date.getMonth() + 1
  MM = MM < 10 ? ('0' + MM) : MM
  let d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  let h = date.getHours()
  h = h < 10 ? ('0' + h) : h
  let m = date.getMinutes()
  m = m < 10 ? ('0' + m) : m
  let s = date.getSeconds()
  s = s < 10 ? ('0' + s) : s
  return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
}


export const toHHMMSS = function (ms) {
  let time = new Date(ms)
  // console.log(timeSecond)
  let hour = time.getHours()
  let min = time.getMinutes()
  let second = time.getSeconds()

  if (hour < 10) {
    hour = `0${hour}`
  }
  if (min < 10) {
    min = `0${min}`
  }
  if (second < 10) {
    second = `0${second}`
  }

  let t = `${hour}:${min}:${second}`
  return t
}

export const msToHHMMSS = (ms) => {
  let time = ms / 1000;

  let hour = Math.floor(time / 60 / 60);

  hour = hour.toString().padStart(2, "0");

  let minute = Math.floor(time / 60) % 60;

  minute = minute.toString().padStart(2, "0");

  let second = Math.floor(time) % 60;

  second = second.toString().padStart(2, "0");

  return `${hour}:${minute}:${second}`;
}

/**
 * 
  秒 转 年月日 时分
 */
export const secondToYMDHM = (s) => {
  var m = Math.floor(s / 60)
  var h = Math.floor(s / 3600)
  var d = Math.floor(s / 86400)
  var n = Math.floor(s / (86400 * 30))
  var y = Math.floor(s / (86400 * 365))
  return `${y}-${n}-${d} ${h}:${m}`
}

/**
 * 获取日期范围 的 日期列表
 * @param {*} startTime
 * @param {*} endTime
 */
export const getDateList = (startTime, endTime) => {
  let s = 0
  let r = []
  let end = null
  console.log(startTime, endTime)
  if (toHHMMSS(endTime) === '23:59:59') {
    end = endTime
  } else {
    // end = new Date(toYYYYMMDD(endTime)).getTime() + 24 * 3600 * 1000 - 1
    end = dayEndTime(endTime)
  }
  let start = new Date(startTime).getTime()

  console.log('GET DATE LIST>>', start, end, toYYYYMMDD(start), toYYYYMMDD(endTime))

  let dayNum = (end + 24 * 3600 * 1000 - start) / (24 * 3600 * 1000)
  console.log(dayNum)
  for (let i = 0; i < dayNum - 1; i++) {
    s = start + i * 24 * 3600 * 1000
    r.push(toYYYYMMDD(s))
  }
  console.log(r)
  return r
}
